package com.zsk.gen;

import org.apache.velocity.VelocityContext;

/**
 * 生成Repository
 * 
 * @author keke
 * @date 2020/07/30
 */
public class RepositoryGen {

    GenConfig config;
    String tableName;
    String comment;

    public RepositoryGen(String tableName, String comment, GenConfig config) {
        super();
        this.tableName = tableName;

        this.config = config;
        this.comment = comment;
    }

    public void gen() {

        String entityName = GenUtil.transUnderlineToHump(tableName, true);

        String packageName = config.getBasePackage();
        if (GenUtil.isNotEmpty(config.getModuleName())) {
            packageName += "." + config.getModuleName();
        }

        String entityClassPath = packageName + "." + config.getEntityPackage() + "." + entityName;

        packageName += "." + config.getRepositoryPackage();

        VelocityContext context = VelocityUtil.createContext();
        context.put("author", config.getAuthor());
        context.put("time", GenUtil.getYmd());
        context.put("package", packageName);
        context.put("tableComment", comment);
        context.put("entityClassPath", entityClassPath);
        context.put("entityName", entityName);

        String vmPath = "com/zsk/template/Repository.vm";
        String targetPath =
            "src/main/java/" + packageName.replaceAll("\\.", "/") + "/" + entityName + "Repository.java";

        if (config.isCoverCode() || !GenUtil.fileExist(targetPath)) {
            VelocityUtil.build(vmPath, targetPath, context);
        }

    }

}
